package repository

import (
	"gorm.io/gorm"
	"shop-bean/model"
)

type IBasketRepository interface {
	FindByUserIdShopCart(db *gorm.DB, userId string)
	Create(db *gorm.DB, product *model.Basket) error
	Updates(db *gorm.DB, product *model.Basket) error
	DeleteShopCartItemsByBasketIds(db *gorm.DB, userId string, basketIds []uint) error
	DeleteAllShopCartItems(db *gorm.DB, userId string) error
	UpdateDiscountItemId(db *gorm.DB, userId string) error
}

type BasketRepository struct {
}

func NewBasketRepository() IBasketRepository {
	return &BasketRepository{}
}

func (r *BasketRepository) FindByUserIdShopCart(db *gorm.DB, userId string) {
	/*
	 SELECT tb.*,tb.basket_count as prod_count,tsd.shop_name,IFNULL(ts.pic,tp.pic)AS pic,ts.price,ts.ori_price,tp.brief,ts.properties,ts.prod_name,ts.sku_name
	        FROM tz_basket tb
	        LEFT JOIN tz_shop_detail tsd ON tb.shop_id = tsd.shop_id
	        LEFT JOIN tz_prod tp ON tb.prod_id = tp.prod_id
	        LEFT JOIN tz_sku ts ON tb.sku_id = ts.sku_id
	        WHERE tp.status = 1 AND ts.status =1 AND tb.user_id = #{userId}
	        ORDER BY tb.`basket_id` DESC
	*/

}

func (r *BasketRepository) Create(db *gorm.DB, basket *model.Basket) error {
	return db.Create(&basket).Error
}

func (r *BasketRepository) Updates(db *gorm.DB, product *model.Basket) error {
	return db.Model(&model.Basket{}).Where("basket_id = ?", product.BasketId).Updates(&product).Error
}

//todo https://studygolang.com/articles/27214?fr=sidebar
func (r *BasketRepository) DeleteShopCartItemsByBasketIds(db *gorm.DB, userId string, basketIds []uint) error {
	return db.Where("user_id = ?", userId).Where("basket_id in ?", basketIds).Delete(&model.Basket{}).Error
}

func (r *BasketRepository) DeleteAllShopCartItems(db *gorm.DB, userId string) error {
	return db.Where("user_id = ?", userId).Delete(&model.Basket{}).Error
}

func (r *BasketRepository) UpdateDiscountItemId(db *gorm.DB, userId string) error {
	//return db.Model(&model.Basket{}).Update().Error
	return nil
}
